package com.lyq.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.io.IOUtils;
import org.junit.*;

import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;

public class HDFSClient {

    Configuration conf =null;
    FileSystem fs = null;

    @Before
    public  void before() throws Exception {
        // 1 获取文件系统
        Configuration conf = new Configuration();
        // 配置在集群上运行
        fs = FileSystem.get(new URI("hdfs://linux121:9000"),conf,"root");
    }

    @After
    public  void after() throws Exception {
        fs.close();
    }

    @Test
    public void testMkdirs() throws URISyntaxException, IOException, InterruptedException {

        // 2 创建目录
        fs.mkdirs(new Path("/test6"));

    }

    @Test
    public void testCopyFromLocalFile() throws IOException {
        fs.copyFromLocalFile(new Path("d:/axis.log"),new Path("/test5"));
    }

    @Test
    public void testDelete() throws IOException {
        fs.delete(new Path("/test6"));
    }

    @Test
    public void testListFiles() throws IOException {
        RemoteIterator<LocatedFileStatus> files = fs.listFiles(new Path("/"), true);

        while (files.hasNext()) {
            LocatedFileStatus status = files.next();
            // 输出详情
            // 文件名称
            System.out.println(status.getPath().getName());
            System.out.println(status.getPath());

            // 长度
            System.out.println(status.getLen());
                // 权限
            System.out.println(status.getPermission());
                // 分组
            System.out.println(status.getGroup());
                // 获取存储的块信息
            BlockLocation[] blockLocations = status.getBlockLocations();
            for (BlockLocation blockLocation : blockLocations) {
                // 获取块存储的主机节点
                String[] hosts = blockLocation.getHosts();
                for (String host : hosts) {
                    System.out.println(host);
                }
            }
            System.out.println("-----------华丽的分割线----------");
        }


    }

    @Test
    public void testListStatus() throws IOException {
        FileStatus[] fileStatuses = fs.listStatus(new Path("/"));
        for (FileStatus fileStatus : fileStatuses) {

            if (fileStatus.isFile()){
                System.out.println("f:"+fileStatus.getPath().getName());
            }
            System.out.println("++++++++++++++++++++++++++");
            if (fileStatus.isDirectory()){
                System.out.println("d:"+fileStatus.getPath().getName());
            }

        }
    }


    //文件上传
    @Test
    public void getFileFromHDFS() throws IOException, URISyntaxException, InterruptedException {

        // 1 获取文件系统
        Configuration configuration = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://linux121:9000"),
                configuration, "root");

        FileInputStream in = new FileInputStream(new File("d:/axis.log"));

        FSDataOutputStream out = fs.create(new Path("/test/in.log"));

        IOUtils.copyBytes(in,out,configuration);

        IOUtils.closeStream(in);
        IOUtils.closeStream(out);


    }

    @Test
    public void getFileToHDFS() throws URISyntaxException, IOException, InterruptedException {

        // 1 获取文件系统
        Configuration configuration = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://linux121:9000"),
                configuration, "root");

        FSDataInputStream open = fs.open(new Path("/wcinput"));

        FileOutputStream out = new FileOutputStream(new File("d://wcoutput"));

        IOUtils.copyBytes(open,out,configuration);

        IOUtils.closeStream(open);
        IOUtils.closeStream(out);


    }


    @Test
    public void readFileSeek2() throws Exception{
        // 1 获取文件系统
        Configuration configuration = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://linux121:9000"),
                configuration, "root");


        FSDataInputStream open = fs.open(new Path("/test6"));


        IOUtils.copyBytes(open,System.out,4096,false);
        open.seek(4096);
        IOUtils.copyBytes(open,System.out,4096,false);

        IOUtils.closeStream(open);
    }







}
